package com.xiaoxi.codemodelserver.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaoxi.codemodelserver.domain.dto.BMSDictDTO;
import com.xiaoxi.codemodelserver.domain.vo.BMSDictTree;
import com.xiaoxi.codemodelserver.domain.vo.BMSDictVO;
import com.xiaoxi.codemodelserver.service.BmsDictService;
import com.xiaoxi.common.exception.BMSException;
import com.xiaoxi.common.response.ApiResponse;
import com.xiaoxi.domain.quick.BmsDict;
import com.xiaoxi.logaspectcommon.common.annotation.OperationLog;
import com.xiaoxi.logaspectcommon.common.constant.OperationConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName BmsDictController
 * @Description
 * @Author xiaoxi
 * @Date 2022-11-03
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/quick/api/v1/dict")
public class BmsDictController {

    @Resource
    private BmsDictService bmsDictService;

    /**
     * 新增
     *
     * @param bmsDictVO
     * @return
     */
    @OperationLog(operation = OperationConstants.CODE_CREATE)
    @PostMapping("/add")
    public ApiResponse add(@RequestBody BMSDictVO bmsDictVO) {
        try {
            bmsDictService.add(bmsDictVO);
            return ApiResponse.ofSuccess(bmsDictVO);
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @OperationLog(operation = OperationConstants.CODE_DELETE)
    @GetMapping("/delete/{id}")
    public ApiResponse delete(@PathVariable("id") Long id) {
        try {
            bmsDictService.delete(id);
            return ApiResponse.ofSuccess(id);
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }

    /**
     * 更新
     *
     * @param bmsDictVO
     * @return
     */
    @OperationLog(operation = OperationConstants.CODE_MODIFY)
    @PostMapping("/update")
    public ApiResponse update(@RequestBody BMSDictVO bmsDictVO) {
        try {
            bmsDictService.update(bmsDictVO);
            return ApiResponse.ofSuccess(bmsDictVO);
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }

    /**
     * 查询树状结构
     *
     * @return
     */
    @OperationLog(operation = OperationConstants.CODE_QUERY_TREE)
    @GetMapping("/findListByTree")
    public ApiResponse findListByTree() {
        try {
            List<BMSDictTree> result = bmsDictService.findListByTree();
            return ApiResponse.ofSuccess(result);
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }

    /**
     * 查询某一个字典对应的 key - value
     *
     * @return
     */
    @OperationLog(operation = OperationConstants.CODE_QUERY_PAGE)
    @GetMapping("/findListByPage")
    public ApiResponse findListByPage(BMSDictDTO bmsDictDTO, Pageable pageable) {
        try {
            IPage<BmsDict> result = bmsDictService.findListByPage(bmsDictDTO, pageable);
            return ApiResponse.ofSuccess(result);
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }

    /**
     * ID查询
     *
     * @param id
     * @return
     */
    @OperationLog(operation = OperationConstants.CODE_QUERY_ONE)
    @GetMapping("/findById/{id}")
    public ApiResponse findById(@PathVariable Long id) {
        try {
            BmsDict bmsDict = bmsDictService.findById(id);
            return ApiResponse.ofSuccess(bmsDict);
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }
}
